package com.vehicle.app.config;

import com.alibaba.fastjson.serializer.SerializeFilter;
import com.alibaba.fastjson.serializer.SerializerFeature;
import com.alibaba.fastjson.serializer.ValueFilter;
import com.alibaba.fastjson.support.config.FastJsonConfig;
import com.alibaba.fastjson.support.spring.FastJsonHttpMessageConverter;
import org.springframework.context.annotation.Configuration;
import org.springframework.http.MediaType;
import org.springframework.http.converter.HttpMessageConverter;
import org.springframework.web.servlet.config.annotation.EnableWebMvc;
import org.springframework.web.servlet.config.annotation.InterceptorRegistry;
import org.springframework.web.servlet.config.annotation.PathMatchConfigurer;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurerAdapter;

import java.nio.charset.Charset;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

/**
 * @author shouhan
 * @version 1.0
 * @date 2018/12/28
 * @desc 自定义拦截器
 * @see
 * @since 1.0
 */
@Configuration
@EnableWebMvc
public class WebMvcConfig extends WebMvcConfigurerAdapter {
    /**
     * 1、 extends WebMvcConfigurationSupport
     * 2、重写下面方法; setUseSuffixPatternMatch : 设置是否是后缀模式匹配，如“/user”是否匹配/user.*，默认真即匹配； setUseTrailingSlashMatch :
     * 设置是否自动后缀路径模式匹配，如“/user”是否匹配“/user/”，默认真即匹配；
     */
    @Override
    public void configurePathMatch(PathMatchConfigurer configurer) {
        configurer.setUseSuffixPatternMatch(Boolean.TRUE).setUseTrailingSlashMatch(Boolean.FALSE);
    }


    @Override
    public void addInterceptors(InterceptorRegistry registry) {
        registry.addInterceptor(new MyInterceptorAdapter());
    }

    @Override
    public void configureMessageConverters(List<HttpMessageConverter<?>> converters) {
        super.configureMessageConverters(converters);
        converters.add(messageConverter());
    }


    /**
     * 自定义 http 返回数据转换
     *
     * @return
     */
    public HttpMessageConverter messageConverter() {
        FastJsonHttpMessageConverter fastJsonHttpMessageConverter = new FastJsonHttpMessageConverter();
        FastJsonConfig fastJsonConfig = new FastJsonConfig();
        // 设置字符集
        fastJsonConfig.setCharset(Charset.forName("UTF-8"));
        // 处理日期
        fastJsonConfig.setSerializeFilters(valueFilter());
        // 剔除循环引用、显示null值key
        //QuoteFieldNames———-输出key时是否使用双引号,默认为true
        //WriteMapNullValue——–是否输出值为null的字段,默认为false
        //WriteNullNumberAsZero—-数值字段如果为null,输出为0,而非null
        //WriteNullListAsEmpty—–List字段如果为null,输出为[],而非null
        //WriteNullStringAsEmpty—字符类型字段如果为null,输出为”“,而非null
        //WriteNullBooleanAsFalse–Boolean字段如果为null,输出为false,而非null
        fastJsonConfig.setSerializerFeatures(SerializerFeature.DisableCircularReferenceDetect, SerializerFeature.WriteMapNullValue);

        fastJsonHttpMessageConverter.setFastJsonConfig(fastJsonConfig);

        // 处理中文乱码问题
        List<MediaType> fastMediaTypes = new ArrayList<>();
        fastMediaTypes.add(MediaType.APPLICATION_JSON_UTF8);
        fastJsonHttpMessageConverter.setSupportedMediaTypes(fastMediaTypes);
        return fastJsonHttpMessageConverter;
    }

    // 数据类型处理Filter
    private SerializeFilter valueFilter() {
        ValueFilter filter = (obj, s, v) -> {
            if (v instanceof Date) {
                return ((Date) v).getTime();
            }
            return v;
        };
        return filter;
    }
}